package com.aquan.map_jetpack.ui.components.map_component


import android.annotation.SuppressLint
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.FloatingActionButton
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.aquan.map_jetpack.R
import com.aquan.map_jetpack.ui.components.basic.LifecycleEffect
import com.mapbox.geojson.Point
import com.mapbox.mapboxsdk.camera.CameraPosition
import com.mapbox.maps.*
import com.mapbox.maps.extension.style.expressions.dsl.generated.zoom
import com.mapbox.maps.extension.style.style
import com.mapbox.maps.plugin.Plugin
import com.mapbox.maps.plugin.animation.easeTo
import com.mapbox.maps.plugin.animation.flyTo
import com.mapbox.maps.plugin.attribution.attribution
import com.mapbox.maps.plugin.compass.compass
import com.mapbox.maps.plugin.delegates.listeners.OnMapLoadedListener
import com.mapbox.maps.plugin.gestures.gestures
import com.mapbox.maps.plugin.locationcomponent.LocationComponentPlugin
import com.mapbox.maps.plugin.logo.logo
import com.mapbox.maps.plugin.scalebar.ScaleBarPlugin

@SuppressLint("InflateParams")
@Composable
fun MapViewPage(context: Context = LocalContext.current) {

    val mapbox: MapView by remember {
        mutableStateOf(
            (LayoutInflater.from(context).inflate(R.layout.map, null, false)
                .findViewById(R.id.mapView) as MapView)
        )
    }

    val initialCameraOptions = CameraOptions.Builder()
        .center(Point.fromLngLat(-74.0066, 40.7135))
        .pitch(45.0)
        .zoom(15.5)
        .bearing(-17.6)
        .build()

//    val mapOptions = MapOptions().Builder()

    val mapInitOptions =
        MapInitOptions(context, cameraOptions = initialCameraOptions)
    val mapView = MapView(context, mapInitOptions)


    var lngLat by remember {
        mutableStateOf("${R.string.lng},${R.string.lat}")
    }

    var mapBox: MapboxMap? = null

    LifecycleEffect(
        onStop = {
            Log.i("Aquan", "onStop")
            mapBox?.removeOnMapLoadedListener(mapLoad(mapBox))
        },
        onCreate = {
            Log.i("Aquan", "onCreate")
        },
        onDestroy = {
//            mapBox?.removeOnMapLoadedListener(mapLoad())
            Log.i("Aquan", "onDestroy")
        },
        onResume = {
            Log.i("Aquan", "onResume")
        }
    )

    Box(modifier = Modifier.fillMaxSize()) {
//        MapView(context, mapInitOptions)
        AndroidView(factory = {
            if (mapBox != null) {
                mapbox.apply {

                    gestures.rotateEnabled = true
                    gestures.scrollEnabled = true
                    gestures.pitchEnabled = true
                    gestures.quickZoomEnabled = true

                    // 隐藏logo
                    logo.enabled = false
                    attribution.enabled = false
                    compass.enabled = false

                    // 放大缩小按钮
                    //  scalebar.enabled = true

//                    location2.enabled = true


                    val scaleBarPlugin: ScaleBarPlugin? =
                        getPlugin(Plugin.MAPBOX_SCALEBAR_PLUGIN_ID)
                    // 隐藏 比例尺
                    scaleBarPlugin?.enabled = false

                    mapBox?.loadStyle(
                        style("mapbox://styles/yvhuem78/cl2yjhyab001d15p8ip7gbaql") {
                        }
                    )
                    // 定位插件
                    val locationPlugin: LocationComponentPlugin? =
                        getPlugin(Plugin.MAPBOX_LOCATION_COMPONENT_PLUGIN_ID)

                    locationPlugin.apply {
                        this?.enabled = true
                        this?.pulsingEnabled = true
                    }

                    // 地图加载的监听时间
                    mapBox?.addOnMapLoadedListener(mapLoad(mapBox))
                }
            } else {
                mapbox.apply {

                    mapBox = getMapboxMap().apply {
                    }
                    gestures.rotateEnabled = true
                    gestures.scrollEnabled = true
                    gestures.pitchEnabled = true
                    gestures.quickZoomEnabled = true

                    // 隐藏logo
                    logo.enabled = false
                    attribution.enabled = false
                    compass.enabled = false

                    val scaleBarPlugin: ScaleBarPlugin? =
                        this.getPlugin(Plugin.MAPBOX_SCALEBAR_PLUGIN_ID)
                    // 隐藏 比例尺
                    scaleBarPlugin?.enabled = false

                    mapBox?.loadStyle(
                        style("mapbox://styles/yvhuem78/cl2yjhyab001d15p8ip7gbaql") {
                        }
                    )
                    // 定位插件
                    val locationPlugin: LocationComponentPlugin? =
                        getPlugin(Plugin.MAPBOX_LOCATION_COMPONENT_PLUGIN_ID)

                    locationPlugin.apply {
                        this?.enabled = true
                        this?.pulsingEnabled = true
                    }

                    // 地图加载的监听时间
                    mapBox?.addOnMapLoadedListener(mapLoad(mapBox))
                }
            }

        })
    }


}

// TODO 地图加载监听 加载完成之后可以添加图层样式的内容
@SuppressLint("LogNotTimber")
private fun mapLoad(mapBox: MapboxMap?): OnMapLoadedListener {
//    TODO("Not yet implemented")
    Log.i("===", "地图加载完成")
    val initialCameraOptions = CameraOptions.Builder()
        .center(Point.fromLngLat(113.45164694, 28.44554253))
        .pitch(0.0)
        .zoom(15.5)
        .bearing(0.0)
        .build()
//    CameraOptions cameraOptions =
    return OnMapLoadedListener {
        Log.i("Aquan", it.begin.toString())
        mapBox?.easeTo(cameraOptions = initialCameraOptions)
    }
}


@Preview
@Composable
fun MapPreview() {
    MapViewPage()
}


/// if(contentSc==true&&target=='left'){}
